#include "area.h"

Area::Area(QWidget* parent, int x, int y, int width, int height, int id, int serial)
    : QFrame(parent), _id(id), _serial(serial), _moved(false), _selected(true), _isResizable(true), _color(QColor(0, 85, 127, 255)), _mDown(false)
{
    if( x<0 )
    	x = 0;
    if( y<0 )
    	y = 0;

    setFrameShape(QFrame::NoFrame);
    setFrameShadow(QFrame::Raised);
    setMouseTracking( true );
    setGeometry(x, y, width, height);
    _lastGeometry = this->geometry();

    QPalette palette;
    QBrush brush(_color);
    brush.setStyle(Qt::SolidPattern);
    palette.setBrush(QPalette::Active, QPalette::Base, brush);
    palette.setBrush(QPalette::Active, QPalette::Window, brush);
    palette.setBrush(QPalette::Inactive, QPalette::Base, brush);
    palette.setBrush(QPalette::Inactive, QPalette::Window, brush);
    palette.setBrush(QPalette::Disabled, QPalette::Base, brush);
    palette.setBrush(QPalette::Disabled, QPalette::Window, brush);
    this->setPalette(palette);

    setAutoFillBackground(true);

    select();
}

void Area::mouseReleaseEvent ( QMouseEvent * )
{
    _mDown = false;
    if (!_moved)
        emit deselectOtherAreas(_id);
}

void Area::deSelect()
{
    _selected = false;
    leaveEvent(NULL);
}

void Area::select()
{
    _selected = true;
    this->setFocus();
    enterEvent(NULL);
}

int Area::serial() const
{
    return _serial;
}

bool Area::selected() const
{
    return _selected;
}

int Area::id() const
{
    return _id;
}

void Area::updateCursor(QMouseEvent * e)
{
    if((e->x()>width()-10)&&(e->y()>height()-10)) {
        setCursor(QCursor(Qt::SizeFDiagCursor));
        _mAct = SDOWNRIGHT;
    }
    else if((e->x()>width()-10)&&(e->y()<10)) {
        setCursor(QCursor(Qt::SizeBDiagCursor));
        _mAct = SUPRIGHT;
    }
    else if(e->y()<3) {
        setCursor(QCursor(Qt::SizeVerCursor));
        _mAct = SUP;
    }
    else if(e->y()>height()-3) {
        setCursor(QCursor(Qt::SizeVerCursor));
        _mAct = SDOWN;
    }
    else if(e->x()>width()-3) {
        setCursor(QCursor(Qt::SizeHorCursor));
        _mAct = SRIGHT;
    }
    else if(e->x()<15) {
        setCursor(QCursor(Qt::SizeAllCursor));
        _mAct = DRAG;
    }
}

void Area::stretch ( QMouseEvent * e )
{
    if (!_isResizable) return;
    if(_mAct == SDOWN)
    {
        this->setGeometry(
                this->x(),
                this->y(),
                this->width(),
                this->height()+(e->globalY()-_dPos.y())
        );
    }
    else
    if(_mAct == SUP)
    {
        this->setGeometry(
                this->x(),
                this->y()+(e->globalY()-_dPos.y()),
                this->width(),
                this->height()-(e->globalY()-_dPos.y())
        );
    }
    else
    if(_mAct == SRIGHT)
    {
        this->setGeometry(
                this->x(),
                this->y(),
                this->width()+(e->globalX()-_dPos.x()),
                this->height()
        );
    }
    else
    if(_mAct == SDOWNRIGHT)
    {
        this->setGeometry(
                this->x(),
                this->y(),
                this->width()+(e->globalX()-_dPos.x()),
                this->height()+(e->globalY()-_dPos.y())
        );
    }
    else
    if(_mAct == SUPRIGHT)
    {
        this->setGeometry(
                this->x(),
                this->y()+(e->globalY()-_dPos.y()),
                this->width()+(e->globalX()-_dPos.x()),
                this->height()-(e->globalY()-_dPos.y())
       );
    }
    // Looking if valid streching
    if( height()<25 || width()<75 )
        setGeometry(_lastGeometry);
    else
        _lastGeometry = geometry();
}

void Area::mousePressEvent(QMouseEvent *e)
{
    if ( e->button() == Qt::LeftButton )
    {
        _moved = false;
        //parent->unselectAll(id);
        if(true == _mDown) return;
        _mDown = true; // Pressed
        _dPos = e->globalPos();
        if(e->x()<15) select();
    }
}

void Area::mouseMoveEvent ( QMouseEvent * e )
{
   if(!_mDown)
   {
       updateCursor(e);
       return;
   }
   if(_mAct == DRAG)
   {

       /*if (this->parent()->grid_mode)
       {
           if ((e->globalX())%10==0)
           {
               int mx = 0;
               if (e->globalX()>_dPos.x())
                   mx = 10;
               else if (e->globalX()<_dPos.x())
                   mx = -10;
               this->parent()->moveSelectedAreas(mx, 0);
           }
           if ((e->globalY())%10==0)
           {
               int my = 0;
               if (e->globalY()>_dPos.y())
                   my = 10;
               else if (e->globalY()<_dPos.y())
                   my = -10;
               this->parent()->moveSelectedAreas(0, my);
           }
       }
       else
       */
           /*this->parent()->moveSelectedAreas(e->globalX()-_dPos.x(),
                                             e->globalY()-_dPos.y());*/
       emit areaToBeMoved( e->globalX() - _dPos.x(), e->globalY() - _dPos.y() );
       _moved = true;


   }
   else stretch(e);

   _dPos = e->globalPos();
}

void Area::enterEvent(QEvent *)
{
    if (_selected)
    {
        //setAutoFillBackground(true);
        QPalette palette;
        QBrush brush(_color);
        brush.setStyle(Qt::SolidPattern);
        palette.setBrush(QPalette::Active, QPalette::Base, brush);
        palette.setBrush(QPalette::Active, QPalette::Window, brush);
        palette.setBrush(QPalette::Inactive, QPalette::Base, brush);
        palette.setBrush(QPalette::Inactive, QPalette::Window, brush);
        palette.setBrush(QPalette::Disabled, QPalette::Base, brush);
        palette.setBrush(QPalette::Disabled, QPalette::Window, brush);
        this->setPalette(palette);
    }
}

void Area::leaveEvent(QEvent *)
{
    if(!_selected)
    {
        //setAutoFillBackground(false);
        QPalette palette;
        QColor a_color;
        a_color.setRed(_color.red());
        a_color.setGreen(_color.green());
        a_color.setBlue(_color.blue());
        a_color.setAlpha(200);
        QBrush brush(a_color);
        brush.setStyle(Qt::SolidPattern);
        palette.setBrush(QPalette::Active, QPalette::Base, brush);
        palette.setBrush(QPalette::Active, QPalette::Window, brush);
        palette.setBrush(QPalette::Inactive, QPalette::Base, brush);
        palette.setBrush(QPalette::Inactive, QPalette::Window, brush);
        palette.setBrush(QPalette::Disabled, QPalette::Base, brush);
        palette.setBrush(QPalette::Disabled, QPalette::Window, brush);
        this->setPalette(palette);
    }
}

void Area::toGrid()
{
    int ox = this->x()%10;
    if (ox >= 5)
        this->setGeometry(this->x() + (10-ox) , this->y(), this->width(), this->height());
    else
        this->setGeometry(this->x() - ox , this->y(), this->width(), this->height());
    int oy = this->y()%10;
    if (oy >= 5)
        this->setGeometry(this->x() , this->y() + (10-oy), this->width(), this->height());
    else
        this->setGeometry(this->x() , this->y() - oy, this->width(), this->height());
}

void Area::setColor(const QColor &color)
{
    _color = color;
    this->deSelect();
}

void Area::emitDeselectOtherAreas()
{
    emit deselectOtherAreas(_id);
}

